﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using FH.Application.PorductApp.Dtos;
using FH.Core.Domain.IRepositories;
using FH.Core.Entities;

namespace FH.Application.UserApp
{
    /// <summary>
    /// 用户管理服务
    /// </summary>
    public class ProductAppService : IProductAppService
    {
        private readonly IProductRepository _productRepository;
        private readonly ICategoryRepository _categoryRepository;
        public ProductAppService(IProductRepository productRepository
            , ICategoryRepository categoryRepository)
        {
            _productRepository = productRepository;
            _categoryRepository = categoryRepository;
        }
        public void Delete(Guid id)
        {
            _productRepository.Delete(id);
        }

        public void DeleteBatch(List<Guid> ids)
        {
            _productRepository.Delete(it => ids.Contains(it.Id));
        }

        public void DeleteBatchCategory(List<Guid> ids)
        {
            _categoryRepository.Delete(it => ids.Contains(it.Id));
        }

        public void DeleteCategory(Guid id)
        {
            _categoryRepository.Delete(id);
        }

        public ProductDto Get(Guid id)
        {
            return Mapper.Map<ProductDto>(_productRepository.Get(id));
        }

        public CategoryDto GetCategory(Guid id)
        {
            return Mapper.Map<CategoryDto>(_categoryRepository.Get(id));
        }
        /// <summary>
        /// 根据Id获取实体
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public IEnumerable<CategoryDto> GetCategory(Guid[] ids)
        {
            return Mapper.Map<List<CategoryDto>>(_categoryRepository.GetAllList(c=>ids.Contains(c.Id)));
        }
        public List<CategoryDto> GetCategoryDtos(int startPage, int pageSize, out int rowCount)
        {
            return Mapper.Map<List<CategoryDto>>(_categoryRepository.LoadPageList(startPage, pageSize, out rowCount, null, it => it.SortId));
        }

        public List<ProductDto> GetProducts(Guid? categoryId, int startPage, int pageSize, out int rowCount)
        {
            if (categoryId.HasValue)
            {
                return Mapper.Map<List<ProductDto>>(_productRepository.LoadPageList(startPage, pageSize, out rowCount, c=>c.CategoryId==categoryId.Value, it => it.SortId));
            }
            else
            {
                return Mapper.Map<List<ProductDto>>(_productRepository.LoadPageList(startPage, pageSize, out rowCount, null, it => it.SortId));
            }
          
        }

        public ProductDto InsertOrUpdate(ProductDto dto)
        {
            if (Get(dto.Id) != null)
                _productRepository.Delete(dto.Id);
            var user = _productRepository.InsertOrUpdate(Mapper.Map<Product>(dto));
            return Mapper.Map<ProductDto>(user);
        }

        public CategoryDto InsertOrUpdateCategory(CategoryDto dto)
        {
            if (GetCategory(dto.Id) != null)
                _categoryRepository.Delete(dto.Id);
            var user = _categoryRepository.InsertOrUpdate(Mapper.Map<Category>(dto));
            return Mapper.Map<CategoryDto>(user);
        }
    }
}
